package com.ruoyi.mq.service.serviceImpl.Processor;

import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.core.domain.entity.SysDept;

import com.ruoyi.mq.service.MessageProcessor;
import com.ruoyi.system.domain.GwHomeSummarize;
import com.ruoyi.system.domain.SysCompany;
import com.ruoyi.system.service.IGwHomeSummarizeService;
import com.ruoyi.system.service.ISysCompanyService;
import com.ruoyi.system.service.ISysDeptService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 处理公司信息
 */
@Component
public class CompanyInfoProcessor implements MessageProcessor {

    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private IGwHomeSummarizeService gwHomeSummarizeService;
    @Autowired
    private ISysCompanyService sysCompanyService;

    @Transactional
    public void processMessage(JSONObject message) {
        // 处理公司信息

        String tag = message.getString("tag");
        String niot_id = message.getString("niot");
        //获取对象数据
        String data = message.getString("data");
        // 创建 ObjectMapper 实例
        ObjectMapper objectMapper = new ObjectMapper();
        switch (tag){
            //新增企业和节点
            case "add":
                try {
                    // 转换为自定义对象
                    SysCompany sysCompany = objectMapper.readValue(data, SysCompany.class);
                    //获取父节点的名称
                    SysDept dept = deptService.selectDeptById(sysCompany.getParentId());
                    //设置归属节点
                    sysCompany.setHomeNode(dept.getDeptName());
                    //新增公司
                    sysCompanyService.insertSysCompany(sysCompany);
                    //创建对象
                    SysDept sysDept = new SysDept();
                    //拷贝对象到汇总表
                    BeanUtils.copyProperties(sysCompany, sysDept);
                    //设置节点标识符
                    sysDept.setNodeIdentifier(message.getString("niot"));
                    GwHomeSummarize gwSummarize = new GwHomeSummarize();
                    //设置汇总表id为新增企业和节点的id,方便公司数量删除
                    gwSummarize.setDeptId(sysDept.getDeptId());
                    //设置节点标识
                    gwSummarize.setNodeIdentifier(niot_id);
                    //新增公司数量
                    gwSummarize.setCompaniesNumber(1L);
                    gwSummarize.setCreateTime(new Date());
                    //新增节点数据
                    gwHomeSummarizeService.insertGwHomeSummarize(gwSummarize);

                    //新增节点数据
                    deptService.insertOutDept(sysDept);
                } catch (JsonProcessingException e) {
                    throw new RuntimeException(e);
                }
                break;
            //删除企业和节点
            case "delete":
                //删除节点表
                deptService.deleteDeptById(Long.valueOf(data));
                //删除企业表
                sysCompanyService.deleteSysCompanyByDeptId(Long.valueOf(data));
                //删除首页汇总表
                gwHomeSummarizeService.deleteGwHomeSummarizeByDeptId(Long.valueOf(data));
                break;
            //修改企业和节点
            case "update":

                try {
                    // 转换为自定义对象
                    SysCompany sysCompany = objectMapper.readValue(data, SysCompany.class);

                    //修改企业表
                    sysCompanyService.updateSysCompany(sysCompany);
                    //创建对象
                    SysDept sysDept = new SysDept();
                    //拷贝对象到节点表
                    BeanUtils.copyProperties(sysCompany, sysDept);
                    //设置节点标识符
                    sysDept.setNodeIdentifier(message.getString("niot"));
                    //新增节点数据
                    deptService.updateDept(sysDept);
                } catch (JsonProcessingException e) {
                    throw new RuntimeException(e);
                }
                break;
            default:
                break;
        }


    }
}
